package com.brillilab.starter.controller.aio.home;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.vo.comments.CommentsGuideVo;
import com.brillilab.domain.vo.experiments.ExperimentDateRangeStatVo;
import com.brillilab.domain.vo.home.HomeBannerVo;
import com.brillilab.domain.vo.home.HomeExperimentGuideMembersVo;
import com.brillilab.domain.vo.home.HomeExperimentGuideVo;
import com.brillilab.domain.vo.home.HomeInviteCodeInfo;
import com.brillilab.domain.vo.home.HomeMaterielVo;
import com.brillilab.domain.vo.home.HomeMemberDynamicVo;
import com.brillilab.domain.vo.home.HomeMemberExperimentCompleteStatVo;
import com.brillilab.domain.vo.home.HomeUserInfoVo;
import com.brillilab.domain.vo.home.HomeWeekPlanVo;
import com.brillilab.domain.vo.kit.KitUsageChartVo;
import com.brillilab.domain.vo.notice.NoticeVo;
import com.brillilab.domain.vo.topics.TopicExperimentVo;
import com.brillilab.domain.vo.topics.TopicScheduleVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.user.IInviteCodeService;
import com.brillilab.service.logic.experiments.ExperimentLogic;
import com.brillilab.service.logic.home.HomeLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 首页
 *
 * @author lj
 */
@RestController
@RequestMapping("/api/home")
public class HomeController {

	@Resource
	private UsersLogic usersLogic;
	@Resource
	private IInviteCodeService inviteCodeService;
	@Resource
	private HomeLogic homeLogic;
	@Resource
	private ExperimentLogic experimentLogic;

	/**
	 * 首页banner列表
	 *
	 * @return
	 */
	@GetMapping("/banner")
	public ResponseVo bannerList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		List<HomeBannerVo> bannerList = homeLogic.getBannerList(token);
		return ResponseVo.success(bannerList);
	}

	/**
	 * 首页用户信息
	 *
	 * @param token
	 * @return
	 */
	@GetMapping("/user")
	public ResponseVo userInfo(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		HomeUserInfoVo info = homeLogic.getUserInfo(userVo);
		return ResponseVo.success(info);
	}

	/**
	 * 首页公告列表
	 *
	 * @return
	 */
	@GetMapping("/notice")
	public ResponseVo noticeList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		PageVo<NoticeVo> page = homeLogic.getNoticeList(userVo);
		return ResponseVo.success(page.getContent());
	}

	/**
	 * 我的物料
	 *
	 * @param token
	 * @return
	 */
	@GetMapping("/materiel")
	public ResponseVo myMateriel(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		HomeMaterielVo merielVo = homeLogic.getMyMateriel(userVo);
		return ResponseVo.success(merielVo);
	}

	/**
	 * 周计划
	 *
	 * @param token
	 * @return
	 */
	@GetMapping("/weekPlan")
	public ResponseVo myWeekPlan(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		HomeWeekPlanVo myThisWeekPlan = homeLogic.getMyThisWeekPlan(userVo.getLabMemberId());
		return ResponseVo.success(myThisWeekPlan);
	}

	/**
	 * 成员计划完成情况统计
	 *
	 * @param token
	 * @return
	 */
	@Deprecated
	@PostMapping("/memberPlanStat")
	public ResponseVo myMemberPlanStat( @RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		
		Date startDa = DateUtil.beginOfWeek(new Date());
		Date endDa = DateUtil.endOfWeek(new Date());
		
		HomeMemberExperimentCompleteStatVo experimentMemberCompleteStat = homeLogic
				.getExperimentMemberCompleteStat(userVo, startDa, endDa);
		return ResponseVo.success(experimentMemberCompleteStat);
	}
	
	/**
	 * 成员计划完成情况统计
	 *
	 * @param token
	 * @return
	 */
	@GetMapping("/memberPlanStat")
	public ResponseVo myMemberPlanStat(String startDate, String endDate, @RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		
		Date startDa = StrUtil.isNotBlank(startDate)? DateUtil.parse(startDate) : DateUtil.beginOfWeek(new Date());
		Date endDa = StrUtil.isNotBlank(endDate) ? DateUtil.parse(endDate) : DateUtil.endOfWeek(new Date());
		
		HomeMemberExperimentCompleteStatVo experimentMemberCompleteStat = homeLogic
				.getExperimentMemberCompleteStat(userVo, startDa, endDa);
		return ResponseVo.success(experimentMemberCompleteStat);
	}

	/**
	 * 我的今日实验
	 *
	 * @param token
	 * @return
	 */
	@PostMapping("/todayExperiment")
	public ResponseVo myTodayExperiment(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		List<TopicExperimentVo> todayExperimentList = homeLogic.getTodayExperimentList(userVo);
		return ResponseVo.success(todayExperimentList);
	}

	/**
	 * 首页近期实验统计
	 *
	 * @param token
	 * @return
	 */
	@PostMapping("/experimentStat")
	public ResponseVo myExperimentStat(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		List<ExperimentDateRangeStatVo> experimentDateRangeStat = homeLogic
				.getExperimentDateRangeStat(userVo.getLabMemberId());
		return ResponseVo.success(experimentDateRangeStat);
	}

	/**
	 * 首页近期试剂取用统计
	 *
	 * @param token
	 * @return
	 */
	@PostMapping("/kitTakeStat")
	public ResponseVo myKitTakeStat(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userVo = usersLogic.getUserByToken(token);
		List<KitUsageChartVo> myKitTakeStat = homeLogic.getMyKitStat(userVo.getLabId(), userVo.getLabMemberId());
		return ResponseVo.success(myKitTakeStat);
	}

	/**
	 * @Description: 获取实验台数据
	 * @Param:
	 */
	@GetMapping("/getExperimentPlatData")
	public ResponseVo getExperimentPlatData(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		Map<String, Integer> result = homeLogic.getExperimentPlatData(userInfoVo);
		return ResponseVo.success(result);
	}


	/**
	 * 首页实验指导-评论列表
	 *
	 * @param token
	 * @return
	 */
	@GetMapping("/experimentCommentList")
	public ResponseVo getCommentsGuideList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<CommentsGuideVo> experimentCommentList = homeLogic.getCommentsGuideList(userInfoVo);
		return ResponseVo.success(experimentCommentList);
	}


	/**
	 * 首页实验指导-成员列表
	 *
	 * @param token
	 * @return
	 */
	//下一版发版删除
	@Deprecated
	@GetMapping("/experimentGuideMemberList")
	public ResponseVo experimentGuideMemberList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<HomeExperimentGuideMembersVo> experimentGuidMemberList = homeLogic.getExperimentGuidMemberList(userInfoVo);
		return ResponseVo.success(experimentGuidMemberList);
	}
	
	/**
	 * 首页实验指导-成员列表
	 *
	 * @param token
	 * @return
	 */
	@GetMapping("/expGuideMemberList")
	public ResponseVo expGuideMemberList(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		List<HomeExperimentGuideMembersVo> experimentGuidMemberList = homeLogic.getGuideMemberList(userInfoVo);
		return ResponseVo.success(experimentGuidMemberList);
	}

	/**
	 * 首页实验指导-成员动态列表
	 *
	 * @param token
	 * @return
	 */
	@Deprecated
	@GetMapping("/memberDynamicList")
	public ResponseVo memberDynamicList(Long labMemberId, @RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.notNull(labMemberId, "成员ID不能为空！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		HomeMemberDynamicVo homeMemberDynamicVo = homeLogic.getMemberDynamicInfo(labMemberId, userInfoVo);
		return ResponseVo.success(homeMemberDynamicVo);
	}

	/**
	 * 首页实验指导-实验列表
	 *
	 * @param token
	 * @return
	 */
	@GetMapping("/experimentGuideList")
	public ResponseVo experimentGuideList(Long labMemberId,
			@RequestHeader(value = StarterConstant.TOKEN) String token) {
		Assert.notNull(labMemberId, "成员ID不能为空！");
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		HomeExperimentGuideVo guideVo = homeLogic.getExperimentGuideList(labMemberId, userInfoVo);
		return ResponseVo.success(guideVo);
	}

	@GetMapping("/getInviteCodeInfo")
	public ResponseVo getInviteCodeInfo(@RequestHeader(value = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		Assert.isTrue(userInfoVo.getIsOwner(), "非PI用户禁止调用此接口！");
		HomeInviteCodeInfo inviteCodeInfo = usersLogic.getInviteCodeInfo(userInfoVo);
		return ResponseVo.success(inviteCodeInfo);
	}

	/**
	 * 获取课题进度
	 *
	 * @param token
	 * @return
	 */
	@GetMapping("/getTopicSchedule")
	public ResponseVo getTopicSchedule(@RequestHeader(value = StarterConstant.TOKEN) String token, int pageNum,
			int pageSize) {
		UserInfoVo userInfoVo = usersLogic.getUserByToken(token);
		PageVo<TopicScheduleVo> page = homeLogic.getTopicSchedule(userInfoVo, pageNum, pageSize);
		return ResponseVo.success(page);
	}

}
